#!/bin/bash

###################################################################
# This file is part of the Emulex Linux Device Driver for         #
# Fibre Channel Host Bus Adapters.                                #
# Copyright (C) 2003-2006 Emulex.  All rights reserved.           #
# EMULEX and SLI are trademarks of Emulex.                        #
# www.emulex.com                                                  #
#                                                                 #
# This program is free software; you can redistribute it and/or   #
# modify it under the terms of version 2 of the GNU General       #
# Public License as published by the Free Software Foundation.    #
# This program is distributed in the hope that it will be useful. #
# ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          #
# WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  #
# FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      #
# DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD #
# TO BE LEGALLY INVALID.  See the GNU General Public License for  #
# more details, a copy of which can be found in the file COPYING  #
# included with this package.                                     #
###################################################################
#
# install script for Emulex Application Helper Module component.
#

# Exit Codes:
#   0  Success.
#   5  Invalid command line parameter.
#  10  Success with warnings.
#  30  Error stopping HBAnyware.
#  35  Error untarring Application Helper Module package.
#  40  Application Helper Module needs to be installed before
#       '--upgradekernel' option can be used.

LOGFILE="/usr/src/lpfcdfc/utils-install.log"

# Function:  log_message()
#
# Description:
#   Print the strings passed as parameters to both STDOUT as well as
#   to a log file.
#
# Parameters:
#   A series of strings.
#
# Returns:
#   0 on success.
log_message()
{
    local ECHO_OPTIONS
    LOGDIRECTORY=${LOGFILE%/*}
    if [ ! -d ${LOGDIRECTORY} ] ; then
	mkdir -p ${LOGDIRECTORY}
    fi

    if [ $# -eq 0 ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ -z "$1" ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ "$1" = "-init" ] ; then
	echo "$0 - $(date)" >> ${LOGFILE}
    else
	while [ -n "$1" ] ; do
	    if [ "${1:0:1}" = "-" ] ; then
		ECHO_OPTIONS="${1}"
	    else
		echo ${ECHO_OPTIONS} "$1" | tee -a ${LOGFILE}
	    fi
	    shift
	done
    fi

    return 0
}

exec_command()
{
    local TEMPFILE
    RETURNVALUE=0
    TEMPFILE=`mktemp /tmp/elx-lpfc-install.XXXXXX`
    if [ "$2" = background ] ; then
	(eval $1 ; echo "RETURNVALUE=$?" >> ${TEMPFILE}) &
	while [ -e ${TEMPFILE} ] && [ ! -s ${TEMPFILE} ] ; do
	    sleep 3
	    log_message -en "."
	done
	. ${TEMPFILE}
	rm -f ${TEMPFILE}
    else
	eval "$1"
	RETURNVALUE=$?
    fi
    log_message -en "\n"
    return ${RETURNVALUE}
}

# Function:  print_help()
#
# Description:
#   Print usage information for this script.
#
# Parameters:
#   none.
#
# Returns:
#   nothing.
#
print_help()
{
    log_message ""
    log_message "$0 will by default install the Emulex Linux"
    log_message "Fibre Channel Application Helper Module.  The following"
    log_message "options may also be used:"
    log_message ""
    log_message "-h,--help            - Print this help message."
    log_message "-i,--install         - Install the Application Helper Module component."
    log_message "-u,--uninstall       - Uninstall a previously installed Application"
    log_message "                        Helper Module component."
    log_message "-k,--upgradekernel   - Install a previously installed Application"
    log_message "                        Helper Module component into an upgraded kernel."
}

# Function:  stop_hbanyware()
#
# Description:
#   Execute /usr/sbin/hbanyware/stop_hbanyware if that script exists.
#
# Parameters:
#   none.
#
# Returns:
#   0  Success.
#  30  Error stopping HBAnyware.
stop_hbanyware()
{
    STOPAPPS="/usr/sbin/hbanyware/stop_hbanyware"

    # Make sure HBAnyware is stopped.
    if [ -x ${STOPAPPS} ] ; then
	log_message "Stopping HBAnyware ..."
	exec_command "${STOPAPPS}"
	let RETURN_VALUE=$?
	if [ ${RETURN_VALUE} -ne 0 ] ; then
	    log_message "Error ${RETURN_VALUE} occurred while stopping HBAnyware."
	    return 30
	fi
    fi

    return 0
}

# Function:  install_kit()
#
# Description:
#   Untars and installs the Application Helper Module component.
#
# Parameters:
#   none.
#
# Returns:
#   0  Success.
#  10  Success with warnings.
#  30  Error stopping HBAnyware.
#  35  Error untarring Application Helper Module package.
install_kit()
{
    IOCTL_KIT=lpfc_2.6_ioctl_module_kit-*.tar.gz

    exec_command "stop_hbanyware" || return $?
    tarball=$(find . -name $IOCTL_KIT)
    exec_command "tar zxf $tarball"
    RETURN_VALUE=$?
    if [ ${RETURN_VALUE} -ne 0 ] ; then
	log_message "Error untarring Applicaton Helper Module package ${IOCTL_KIT}."
	log_message "Error code:  ${RETURN_VALUE}"
	return 35
    fi

    ${tarball%.tar.gz}/ioctl-install
    RETURN_VALUE=$?
    if [ ${RETURN_VALUE} -ne 0 ] ; then
	if [ ${RETURN_VALUE} -ne 20 ] ; then
	    log_message "Error installing Application Helper Module."
	    return ${RETURN_VALUE}
	fi
	return 10
    fi

    return 0
}

# Function:  uninstall_kit()
#
# Description:
#   Uninstalls the Application Helper Module component if the ioctl-install
#   script exists in the /usr/src/lpfcdfc/ directory.
#
# Parameters:
#   none.
#
# Returns:
#   0  Success.
#  10  Success with warnings.
#   nonzero if an error occurs while uninstalling the Application Helper
#    Module.
#
uninstall_kit()
{
    UNINSTALLER="/usr/src/lpfcdfc/ioctl-install"
    TEMPPATH="/tmp"

    exec_command "stop_hbanyware" || return $?
    if [ -e ${UNINSTALLER} ] ; then
	${UNINSTALLER} --uninstall
	let RETURN_VALUE=$?
	if [ ${RETURN_VALUE} -ne 0 ] ; then
	    if [ ${RETURN_VALUE} -ne 25 ] ; then
		log_message "Error uninstalling Application Helper Module."
		return ${RETURN_VALUE}
	    fi
	    return 10
	fi
    else
	log_message "Application Helper Module not installed."
    fi

    return 0
}

# Function:  upgrade_kernel()
#
# Description:
#   Installs a previously installed Application Helper Module into an
#   upgraded kernel.
#
# Parameters:
#   none.
#
# Returns:
#   0  Success.
#  40  Application Helper Module must be installed.
#   nonzero if an error occurs while upgrading the Application Helper
#    Module.
#
upgrade_kernel()
{
    if [ ! -e /usr/src/lpfcdfc/ioctl-install ] ; then
	log_message "The Application Helper Module kit must be installed for"
	log_message "this option to make sense.  Please install the Application"
	log_message "Helper Module before running with the '--upgradekernel'"
	log_message "option."
	return 40
    else
	/usr/src/lpfcdfc/ioctl-install --upgradekernel
	let RETURN_VALUE=$?
	if [ ${RETURN_VALUE} -ne 0 ] ; then
	    log_message "Error ${RETURN_VALUE} occurred while installing the"
	    log_message "Application Helper Module into an upgraded kernel."
	fi
	return $?
    fi
}

# Initialize the log file.
log_message -init
# Process command line parameters.
if [ $# -eq 0 ] ; then
    SCRIPT_COMMAND="install"
elif [ $# -eq 1 ] ; then
    case "$1" in
	-i|--install)
	    SCRIPT_COMMAND="install"
	    ;;
	-k|--upgradekernel)
	    SCRIPT_COMMAND="upgradekernel"
	    ;;
	-u|--uninstall)
	    SCRIPT_COMMAND="uninstall"
	    ;;
	-h|--help)
	    print_help
	    exit 0
	    ;;
	*)
	    log_message "Invalid option - $1"
	    print_help
	    exit 5
	    ;;
    esac
    shift
else
    log_message "Too many parameters specified."
    print_help
    exit 5
fi

# cd to the package directory
cd ${0%/*}

let EXIT_VALUE=0
if [ -z "${SCRIPT_COMMAND}" ] ; then
    log_message "Fatal error - SCRIPT_COMMAND has no value."
    exit 255
elif [ ${SCRIPT_COMMAND} = "uninstall" ] ; then
    uninstall_kit
    let EXIT_VALUE=$?
elif [ ${SCRIPT_COMMAND} = "install" ] ; then
    install_kit
    let EXIT_VALUE=$?
elif [ ${SCRIPT_COMMAND} = "upgradekernel" ] ; then
    upgrade_kernel
    let EXIT_VALUE=$?
else
    log_message "Fatal error - invalid value for SCRIPT_COMMAND."
    exit 255
fi

exit ${EXIT_VALUE}
